-
Notifications
You must be signed in to change notification settings - Fork 534
add optional strict check for printf parameter types #4349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add optional strict check for printf parameter types #4349
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems a bit long and it's not completely accurate either (it accepts classes with __toString()
even if they don't implement Stringable
). The other alternative I considered was just string
, but that might be a bit confusing to users. Is there anything better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's try __stringandstringable
. I can see you're already using new StringAlwaysAcceptingObjectWithToStringType
in a related place.
Yes, please contribute seting thing parameter to true in phpstan-strict-rules once this is released 😊
f6ed272
into
phpstan:2.1.x
Thank you!
Please also send a docs update for phpstan.org about checkStrictPrintfPlaceholderTypes
. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@schlndh @ondrejmirtes After enabling this rule I noticed a lot of errors related to string|null
types.
Personally, I think replacing those sprintf
call arguments with $value ?? ''
feels like making a computer happy. As it does exactly the same as what sprintf
does for %s
.
See https://3v4l.org/TlHU9#v8.4.13
I know this is a strict rule, but wonder if this was really intentional or something that should be reconsidered?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like it's useful that PHPStan lets you know you're potentially passing null
to sprintf
. You might want to deal with null
differently than what sprintf
does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I intentionally implemented it like that to match my understanding of "strict". However, I encountered the same thing: hundreds of issues like this, most of which I don't care about (though I'm fine with letting them all fall into the baseline). So I would be open to changing it. I'm just not sure what's the best way to go about it.
Strict-rules do allow $null . $string
and "{$null}"
, so I guess this wouldn't have to be as strict either (unless it's allowed by omission rather than intentionally). At the very least it's a bit inconsistent.
On the other hand, there are third-party rules (e.g. shipmonk rules) which do prohibit it, so clearly there is also interest in the fully strict version.
Another thing to consider is whether it should be allowed only in %s
or also other placehoders?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strict-rules do allow
$null . $string
and"{$null}"
, so I guess this wouldn't have to be as strict either (unless it's allowed by omission rather than intentionally). At the very least it's a bit inconsistent.
This is an excellent point.
Another thing to consider is whether it should be allowed only in
%s
or also other placehoders?
I would say only for %s
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An idea: string|null should be reported only on level 8+, basicallh RuleLevelHelper should be utilized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, that won't help me (level 9 🙈)
@angeleg
angeleg
Sep 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say only for
%s
.
To expand on that point: %d
+ null
make 0
, which is confusable with, well, passing an actual 0
integer. 😄 So for that reason I wouldn't allow it either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ondrejmirtes Re: level 8
- report calling methods and accessing properties on nullable types
IMO that's stretching it a little bit. And it wouldn't be consistent with how string + null is handled in other cases (concatenation, interpolation, ...).
Let's consider consistency with phpstan-strict-rules (playground):
- Strict-rules prohibit
bool
andnull
in numerical operations (consistent with%d
and%f
). - Strict-rules allow
numeric-string
in numerical operations (inconsistent with%f
,%d
is probably OK since float is excluded as well). - Strict-rules prohibit general string in numerical operations (consistent with
%d
and%f
). - Strict-rules allow
bool
and null in string operations (inconsistent with%s
).
Idea:
- Let's enable passing
null
to%s
. If PHPStan later adds a config option to limit type coercions then it could be used here (as well as$string . $null
,"{$null}"
, ...). - Let's enable passing
numeric-string
to%f
. I excluded it, becausenumeric-string
could lose precision by doing so. But it's probably too strict. - Let's keep passing
bool
to%s
prohibited. I could later try to prohibitbool
in$string . $bool
and"{$bool}"
in strict-rules (no promises).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a small side note, PHPStorm will tell me to remove (string) $null
for %s
in a sprintf
. It's only fine with $null ?? ''
.
@derrabus
derrabus
Sep 30, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Idea:
- Let's enable passing
null
to%s
. If PHPStan later adds a config option to limit type coercions then it could be used here (as well as$string . $null
,"{$null}"
, ...).
I don't know if that helps, but I've reviewed the new errors that popped up in my current project. The dominant error reported was that we pass something that could be null. And in most of the cases, it would be a problem is the value was actually null
. It was also old news because we already knew that the code in question is a bit too lax when it comes to handling null
. So all errors that the new rule added to our baseline were actually just "more of the same".
- Let's enable passing
numeric-string
to%f
. I excluded it, becausenumeric-string
could lose precision by doing so. But it's probably too strict.
Yes, it is. In fact, you actually might lose precision when casing a numeric string to a float. And besides it doesn't make much sense to cast a string to a float just to format it as a string again.
heiglandreas
commented
Sep 29, 2025
I just stumbled upon this and - what shall I say - the usability seems rather limited. Especially when using the same parameter multiple times in the format string.
https://phpstan.org/r/470b0175-9359-418e-87d5-3a37013bccae
Why does an int not need to be cast to string when the format asks for a string? Why does an int not need to be cast to a float?
I do understand that it can make sense to hint that the formatting parameter will cast a value to something else. But the messages are actually wrong:
For one thing: Parameter #2 of function sprintf is expected to be float by placeholder #3 ("%10ドル.02f")
- This is placeholder 1 we are talking about.
And the other thing is that sprintf does not expect the value to be float. It merely will format the value as float. That is a small but important difference! sprinf
expects anything. But it will make sure that the passed parameter will be cast to the expected format when integrated into the string according to the rules noted in the format.
And as https://phpstan.org/r/83f7819b-7fef-46dd-a397-c38506bad3b3 and https://phpstan.org/r/ab740815-f7c1-41a8-96ad-2b4d508ca3c9 show it does not apply these rules in every situation. Here I would expect PHPStan to trigger an error whenever something is passed that does not work with the provided type.
In the second example I would also expect PHPStan to understand the function signature that is shown in the PHP documentation ( sprintf(string $format, mixed ...$values): string
especially as bool
and null
are nowhere usable in the formatter-strings, so the values should not be allowed at all - according to the logic that only values are allowed that the formatting string actually expects...
@heiglandreas I think it's fine, you don't need to turn on the strict version if you don't want it?
heiglandreas
commented
Sep 29, 2025
I am more concerned about a false sense of "safety" that this rule implies.
Even if I want a very strict type-checking, this does - at least in the current way - not add a reliable safety to the sprintf
-function.
- hard-coded formatting-strings that are passed as a parameter are not taken into account
- mixed-parameters that require determining the parameter type based on the passed value are not supported
- argumend-numbering - especially with multiple usages of the same argument - are not taken into account.
So enabling that in the strict-checking by default seems ... an interesting choice. Now one has to explicitly remove this very opinionated and brittle check. Instead of allowing one to opt-in if that is what I want and when I exactly know what to do.
derrabus
commented
Sep 30, 2025
So enabling that in the strict-checking by default seems
Strict rules are not enabled by default, are they?
heiglandreas
commented
Sep 30, 2025
If you enable the strict rules, then this rule is also enabled. As I explained above the rule is not really checking in all circumstances and giving a very skewed result.
I can see that it might make sense in certain circumstances. But for sure not in all situations where one wants to enable a strict check.
- This is in bleeding edge which means things are not set in stone and feedback is very appreciated.
- strict-rules are by definition opinionated.
heiglandreas
commented
Sep 30, 2025
Opinionated is fine, lulling into a false sense of security is ... arguable? 😁
Closes phpstan/phpstan#13496
Previous PR: #3977
Would you accept turning this on by default in phpstan-strict-rules?